<template>
  <div>
    <el-form inline :model="searchForm">
      <el-form-item>
        <el-input v-model="searchForm.shopName" clearable placeholder="店铺名字"></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchForm.status" clearable placeholder="信息状态">
          <el-option label="可用" :value="1"/>
          <el-option label="禁用" :value="0"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList">查询</el-button>
        <el-button type="primary" @click="showDialog()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border>
      <el-table-column prop="shopId" label="编号" align="center" header-align="center" width="80"/>
      <el-table-column prop="shopName" label="店铺名字" align="center" header-align="center" width="150" show-overflow-tooltip/>
      <el-table-column prop="address" label="店铺地址" align="center" header-align="center"/>
      <el-table-column prop="status" label="状态" align="center" header-align="center" width="130">
        <template slot-scope="scope">
          <el-button title="点击更改状态" v-if="scope.row.status === 0" type="danger" size="small" @click="changeStatus(scope.row.shopId,1)">下架</el-button>
          <el-button @click="changeStatus(scope.row.shopId,0)" type="success" v-else size="small" title="点击更改状态">正常</el-button>
        </template>
      </el-table-column>
      <el-table-column label="查看" width="220" align="center" header-align="center" fixed="right">
        <template slot-scope="scope">
          <el-button size="small" @click="showCabinetList(scope.row.shopId)">包含</el-button>
          <el-button size="small" @click="ShowImageList(scope.row.shopId)">图片</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" header-align="center" fixed="right" width="260">
        <template slot-scope="scope">
          <el-button size="small" @click="showDialog(scope.row.shopId)">修改</el-button>
          <el-button size="small" @click="deleteInfo(scope.row.shopId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="pageIndex" :page-size="pageSize" :total="totalPage" :page-sizes="[10,20,30,50,100,200]" layout="total,sizes,prev,pager,next,jumper" @current-change="currentChange" @size-change="sizeChange"></el-pagination>
    <ShopAddOrUpdate ref="shopAddOrUpdate" @onCloseWin="getDataList"/>
    <ShowCabinetList ref="showCabinetList"/>
    <picture-view :showpage="true" :url-list="imageList"
                  :on-close="closeViewer" v-if="showViewer"></picture-view>
  </div>
</template>

<script>
import ShopAddOrUpdate from '@/views/shop/ShopAddOrUpdate'
import ShowCabinetList from '@/views/shop/ShowCabinetList'
import PictureView from '@/views/shop/PictureView'
export default {
  components: {
    ShowCabinetList,
    ShopAddOrUpdate,
    PictureView
  },
  data () {
    return {
      searchForm: {
        shopName: '',
        status: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      showViewer: false,
      imageList: []
    }
  },
  methods: {
    getDataList () {
      this.dataListLoading = this.$loading({ text: '加载表格...' })
      this.$http.get('/shop/list', {
        page: this.pageIndex,
        limit: this.pageSize,
        ...this.searchForm
      }).then(data => {
        this.dataListLoading.close()
        if (data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        }
      })
    },
    currentChange (val) {
      this.pageIndex = val
      this.getDataList()
    },
    sizeChange (val) {
      this.pageSize = val
      this.getDataList()
    },
    showDialog (shopId) {
      this.$refs.shopAddOrUpdate.init(shopId)
    },

    deleteInfo (shopId) {
      this.$confirm(`确定删除编号${shopId}的数据吗？`).then(() => {
        this.dataListLoading = this.$loading({ text: '加载表格...' })
        this.$http.delete(`/shop/delete/${shopId}`).then(data => {
          this.dataListLoading.close()
          if (data.code === 0) {
            this.$message({
              message: '删除成功！',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$alert(data.msg, { type: 'error' }).then(() => {
              this.getDataList()
            })
          }
        })
      })
    },
    changeStatus (shopId, status) {
      this.$confirm('确定更改状态吗？').then(() => {
        this.$http.post('/shop/status', { shopId, status }).then(data => {
          if (data.code === 0) {
            this.$message.success('更新成功')
            this.getDataList()
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
    showCabinetList (shopId) {
      this.$refs.showCabinetList.init(shopId)
    },
    closeViewer () {
      this.showViewer = false
    },
    ShowImageList (shopId) {
      this.showViewer = true
      this.imageList = [] /* 清空图片列表 */
      this.dataListLoading = this.$loading({ text: '加载中...' })
      this.$http.get('/shop/info/' + shopId).then(data => {
        if (data.code === 0) {
          this.dataListLoading.close()
          console.log(data)
          if (data.list.imageList.length > 0) {
            this.showViewer = true
            this.imageList = data.list.imageList.map(item => {
              return {
                img: process.env.VUE_APP_BASE_API + '/img/' + item.imageUrl,
                info: [{ label: '图片名', value: item.imageUrl }]
              }
            })
          } else {
            this.$message.warning('暂无照片')
          }
        }
      })
    }

  },
  created () {
    this.getDataList()
  }
}
</script>

<style scoped>

</style>
